/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ExerciciosListaDialog.java
 *
 * Created on 03/11/2009, 01:27:38
 */
package interpretadorportugolvisual;

import br.org.isvi.interpretador.portugol.access.IPVBusinessFacade;
import br.org.isvi.interpretador.portugol.model.Aula;
import br.org.isvi.interpretador.portugol.model.Exercicio;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author abraaoisvioliveirasouzadocarmo
 */
public class ExerciciosListaProfessorDialog extends javax.swing.JDialog {

    private Aula aula;

    /** Creates new form ExerciciosListaDialog */
    public ExerciciosListaProfessorDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
//        tblExerciciosAula.setDefaultRenderer(Boolean.class, TableCellRenderer);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblExerciciosAula = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        btnFiltrar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cmbAulas = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        tblExerciciosAula.setModel(obterExerciciosAula());
        tblExerciciosAula.setName("tblExerciciosAula"); // NOI18N
        tblExerciciosAula.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblExerciciosAulaMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblExerciciosAula);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(interpretadorportugolvisual.InterpretadorPortugolVisualApp.class).getContext().getResourceMap(ExerciciosListaProfessorDialog.class);
        tblExerciciosAula.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tblExerciciosAula.columnModel.title0")); // NOI18N
        tblExerciciosAula.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tblExerciciosAula.columnModel.title1")); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel2.border.title"))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N

        btnFiltrar.setText(resourceMap.getString("btnFiltrar.text")); // NOI18N
        btnFiltrar.setName("btnFiltrar"); // NOI18N
        btnFiltrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFiltrarActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        cmbAulas.setModel(new DefaultComboBoxModel(IPVBusinessFacade.obterTodosAula().toArray()));
        cmbAulas.setName("cmbAulas"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(383, Short.MAX_VALUE)
                .addComponent(btnFiltrar)
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbAulas, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(34, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cmbAulas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addComponent(btnFiltrar))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 494, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(6, 6, 6))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 532, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblExerciciosAulaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblExerciciosAulaMouseClicked
//        SessaoAluno.exercicioSelecionado = (Exercicio) ((DefaultTableModel)tblExerciciosAula.getModel()).getValueAt(tblExerciciosAula.getSelectedRow(), 0);
//        new ProblemaDialog(null, true).setTexto(SessaoAluno.exercicioSelecionado.problema).setVisible(true);
//        this.setVisible(false);
}//GEN-LAST:event_tblExerciciosAulaMouseClicked

    private void btnFiltrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFiltrarActionPerformed
        aula = (Aula) cmbAulas.getSelectedItem();
        tblExerciciosAula.setModel(obterExerciciosAula());
//        tblExerciciosAula.revalidate();
//        tblExerciciosAula.repaint();
    }//GEN-LAST:event_btnFiltrarActionPerformed

    public DefaultTableModel obterExerciciosAula() {
        if (aula == null) {
            Exercicio[][] exercicios = new Exercicio[1][2];
            return new ExercicioTableModel(null, new String[]{"Problema", "Liberado"});
        }

        java.util.List<Exercicio> lst = IPVBusinessFacade.obterTodosExerciciosPorAula(aula);
        Exercicio[][] Exercicios = new Exercicio[lst.size()][2];

        for (int i = 0; i < Exercicios.length; i++) {
            Exercicios[i] = new Exercicio[]{lst.get(i)};
        }


        return new ExercicioTableModel(Exercicios, new String[]{"Problema", "Liberado"});
    }

    class ExercicioTableModel extends DefaultTableModel {

        List<Exercicio> nlst;

        public ExercicioTableModel(Object[][] o, Object[] p) {
            super(o, p);
            if (aula != null && o != null) {
                
                nlst = IPVBusinessFacade.obterTodosExerciciosPorAula(aula);
            }
        }

        @Override
        public boolean isCellEditable(int row, int col) {

            return col>0;
        }

        @Override
        public void setValueAt(Object value, int row, int col) {
            if(col > 0) {
                Exercicio ex = ((Exercicio) super.getValueAt(row, 0));

                ex.setLiberado(!ex.isLiberado());
                IPVBusinessFacade.persistir();
            }
        }


        @Override
        public Class getColumnClass(int c) {
            if (c > 0) {
                return Boolean.class;
            } else {
                return Exercicio.class;
            }
        }

        @Override
        public Object getValueAt(int row, int col) {
            if (col > 0) {

                return nlst.get(row).isLiberado();
            } else {
                return nlst.get(row);
            }
        }
    };
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFiltrar;
    private javax.swing.JComboBox cmbAulas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable tblExerciciosAula;
    // End of variables declaration//GEN-END:variables
}
